//
//  CZStatusRetweetView.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZStatusRetweetView: UIView {

   
    
    var retweetStatus: CZStatus? {
        didSet{
            if retweetStatus == nil{
                retweetContentLabel.text = nil
            }else{
                let name = retweetStatus?.user?.screen_name ?? "未知用户名"
                let text = retweetStatus?.text ?? ""
                retweetContentLabel.text = "@\(name): \(text)"
            }
            
            retweetPictureView.pictureURLs = retweetStatus?.pictureURLs
            
            retweetContentLabel.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus == nil ? 0 : CellMargin
                make.top.equalTo(self).offset(margin)
            }
            
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                var margin: CGFloat = CellMargin
                if retweetStatus == nil{
                    margin = 0
                }else if retweetStatus?.pictureURLs == nil || retweetStatus?.pictureURLs?.count == 0 {
                    margin = 0
                }
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
            }
            
            self.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus == nil ? 0 :CellMargin
                make.bottom.equalTo(retweetPictureView).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI(){
        backgroundColor = UIColor.init(white: 237/255.0, alpha: 0.8)
//        retweetPictureView.backgroundColor = UIColor.magentaColor()
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(self).offset(CellMargin)
        }
        
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(CellMargin)
        }
    }
    
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width -  2 * CellMargin
        label.text = "被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容"
        return label
    }()
    
    private lazy var retweetPictureView: CZStatusPictureView = CZStatusPictureView()
}
